package customGUI.selectors;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import db.DBConnectionFactory;
import db.DBConnector;

public abstract class AssetSelector extends JPanel{

	protected JComponent selector;
	
	protected AssetSelector(String label, JComponent selector){
		this.selector = selector;
		
		refreshAssetChoices();
		
		add(new JLabel(label));
		add(selector);
		
		setSize(selector.getWidth(), selector.getHeight());
	}
	
	public void refreshAssetChoices(){
		ResultSet rs;
		String query = getRetrieveAssetQuery();
		
		Connection con = DBConnectionFactory.getInstance().getConnection();
		rs = DBConnector.executeQuery(con, query);
		ArrayList<String> assetList = new ArrayList<String>();
		try {
			addExtraItemsToList(assetList);
			addResultSetItemsToList(assetList, rs);	
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		setChoices(assetList);
	}
	
	public JComponent getSelector(){
		return selector;
	}
	
	public abstract int getSelectedID();
	public abstract String getSelectedName();
	
	protected abstract String getRetrieveAssetQuery();
	protected abstract void addExtraItemsToList(ArrayList<String> assetList);
	protected abstract void addResultSetItemsToList(ArrayList<String> assetList, ResultSet rs);
	protected abstract void setChoices(ArrayList<String> assetList);
	
	
	
		
}
